package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.callhistory.CallHistoryAppInfo;
import com.ucredit.babel.common.model.callhistory.CallHistoryCallDetail;
import com.ucredit.babel.common.model.callhistory.CallHistoryDeviceInfo;
import com.ucredit.babel.common.model.callhistory.CallHistoryRecord;
import com.ucredit.babel.common.model.callhistory.CallHistoryUserInfo;

@Deprecated
public class PhoneCallRecordParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  Text val = new Text();
	private  Text key= new Text();
	private  String user_id="";
	private String timestamstr="";
	private StringBuilder sb = new StringBuilder(500);
	/**
	 * 手机通话记录基础数据
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parsePhoneRecordBase(Context context,CallHistoryRecord call,String successful) throws IOException, InterruptedException{
		
    	 new StringBuilder(200);
    	Job job = call.getJob();

    	CallHistoryUserInfo user= call.getUserInfo();
    	CallHistoryDeviceInfo device = call.getDeviceInfo();
    	CallHistoryAppInfo app = call.getAppInfo();
    	 timestamstr=DateUtil.formateDateStr(new Date(call.getTimestamp()));
    	//主键,身份证+job_id+时间
    	sb.delete(0,sb.length());
    	user_id=generateId(user.getSystemUniqueKey(),job.getJobID(),String.valueOf(timestamstr));
    	sb.append(user_id).append(CRT001).append(user.getSystemUniqueKey()).append(CRT001).append(timestamstr).append(CRT001);
    	//app
    	//app的唯一标识
    	sb.append(app.getAppBundle()==null?"":app.getAppBundle()).append(CRT001);
    	//app版本
    	sb.append(app.getAppVersion()==null?"":app.getAppVersion()).append(CRT001);
    	//是否越狱
    	sb.append(device.getJailbroken()==null?"":device.getJailbroken()).append(CRT001);
    	//分辨率
    	sb.append(device.getScreen()==null?"":device.getScreen()).append(CRT001);
    	sb.append(device.getSimulator()==null?"":device.getSimulator()).append(CRT001);
    	//网络类型
    	sb.append(device.getNetworkType()==null?"":device.getNetworkType()).append(CRT001);
    	//系统
    	sb.append(device.getOs()==null?"":device.getOs()).append(CRT001);
    	//是否wifi
    	sb.append(device.getWifi()==null?"":device.getWifi()).append(CRT001);
    	//设备唯一码
    	sb.append(device.getImei()==null?"":device.getImei()).append(CRT001);
    	//设备名称
    	sb.append(device.getDeviceName()==null?"":device.getDeviceName()).append(CRT001);
    	//语言
    	sb.append(device.getLanguage()==null?"":device.getLanguage()).append(CRT001);
    	//系统版本
    	sb.append(device.getOsVersion()==null?"":device.getOsVersion()).append(CRT001);
    	//model
    	sb.append(device.getModel()==null?"":device.getModel()).append(CRT001);
    	//运营商
    	sb.append(device.getCarrier()==null?"":device.getCarrier()).append(CRT001);

    	//job_id
    	sb.append(job.getJobID()==null?"":job.getJobID()).append(CRT001);
    	//是否结束戳
    	sb.append(job.isFinished()==null?"":job.isFinished()).append(CRT001);
    	//是否请求
    	sb.append(job.isRequested()==null?"":job.isRequested()).append(CRT001);
    	//是否成功
    	sb.append(successful).append(CRT001);
    	//起始时间
    	sb.append(job.getStartTime()==null?"":job.getStartTime()).append(CRT001);
    	//结束时间
    	sb.append(job.getEndTime()).append(CRT001);
    	//数据文件
    	sb.append(job.getDataFile()).append(CRT001);
    	//系统ID
    	sb.append(job.getSystemID()).append(CRT001);
    	//查询编号
    	sb.append(job.getQuery()).append(CRT001);
    	//尝试次数
    	sb.append(job.getTryTimes()==null?"0":job.getTryTimes()).append(CRT001);
    	//JOB类型
    	sb.append("").append(CRT001);
    	
    	key.set("phoneCallJob");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	
	}
	
	private static String generateId(String userkey,String jobid,String ts){
		 
		return MD5Util.getMd5(userkey+jobid+ts);
	}
	private static String generateId(String userkey,String jobid,String ts,String mobile){
		 
		return MD5Util.getMd5(userkey+jobid+ts);
	}
	/**
	 * 通话记录明细
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public   void parsePhoneRecordDetail(Context context, CallHistoryRecord call) throws IOException, InterruptedException{
		
    	StringBuilder sb = new StringBuilder(200);
    	Job job = call.getJob();
    	CallHistoryUserInfo user= call.getUserInfo();
    	List<CallHistoryCallDetail> list =call.getCalls();
    	int i=0;
    	String s="";
    	
    	for(CallHistoryCallDetail c :list){
    		i++;
    		sb.delete(0, sb.length());
    		sb.append(user_id).append(CRT001);
    		//时间戮
    		sb.append(timestamstr).append(CRT001);
    		//序号
    		sb.append(i).append(CRT001);
    		//通话类型
    		sb.append(c.getType()).append(CRT001);
    		//通话联系人
    		sb.append(c.getName()).append(CRT001);
    		//通话号码
    		sb.append(c.getNumber()).append(CRT001);
    		//通话号码类型，如
    		sb.append(c.getNumberType()).append(CRT001);
    		//通话发生时间戳，单位毫秒
    		sb.append(c.getDate()).append(CRT001);
    		//通话时长，单位秒
    		sb.append(c.getDuration());
    		s=sb.toString().replace("\n", " ");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("phoneCallRecords");
        	val.set(s);
        	context.write(key, val);
    	}
    	
	}
}
